Java 程式碼
public class Exercise_2 { public static void main(String[] args) { // state1 String s1 = "lion"; String s2 = "lion"; System.out.println("s1==s2:"+(s1==s2)+"; s1 equals s2:"+s1.equals(s2)); // state2 s1 = new String("lion"); s2 = new String("lion"); System.out.println("s1==s2:"+(s1==s2)+"; s1 equals s2:"+s1.equals(s2)); // state3 s1 = new String("lion"); s2 = new String("sheep"); System.out.println("s1==s2:"+(s1==s2)+"; s1 equals s2:"+s1.equals(s2)); } }
state1: 參考型別字串(String) 在 Stack 與 Heap 中的存放方式